<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CF_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('shicai_mdl');
		$this->load->model('caipu_mdl');
	}

	public function index()
	{
		$this->caipu();
	}
	/**
	 * 菜谱搜索页
	 */
	public function caipu($keywords = '', $page = 1)
	{
        $keywords = $keywords ? $keywords : $this->input->get('keyword');
        if (empty($keywords)) redirect(site_url());
		$this->load->model('Article_mdl');
		$this->load->helper('text');
		$this->uid = $this->user_login->is_logged_in() ? $this->user_login->get_user_id() : 0;

		$page = max(1, intval($page));
		$limit = 12; //每页显示数
		$offset = ($page - 1) * $limit;

	    //WAP分页
	    if ($this->input->get('offset') && $this->input->get('limit')) 
	    {
	      $offset = $this->input->get('offset');
	      $limit = $this->input->get('limit');
	    } 
    
		$query = $this->caipu_mdl->get_caipu_search($keywords, $offset, $limit);
		$search_count = 10000;

		$shicai_query = $this->shicai_mdl->get_shicai_by_title($keywords);
		$article_query = $this->Article_mdl->get_article_search($keywords, $offset, $limit);

		$shicai_xiangke_query = $this->shicai_mdl->get_shicai_xiangke_by_title($keywords);

		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('search/caipu/'.$keywords);
		$config['total_rows'] = $search_count;
		$config['per_page'] = $limit; 
		$config['uri_segment'] = 4;
		$this->pagination->initialize($config);

		$data['shicai_info'] = $shicai_query;

		if ($shicai_query)
		{
			$data['shicai_id'] = $shicai_query['id'];
		}

		$data['xsxk_info'] = $shicai_xiangke_query ? $shicai_xiangke_query : 0;
		$rows_array = array();
		foreach ($query->result_array() as $value) 
		{
			$value['coverImage'] = server_img($value['coverImage']);
			$rows_array[] = $value;
		}
		$data['search_list'] = $rows_array;
		$data['article_list'] = $article_query->result_array();
		
		$data['search_list_count'] = $search_count;
		$data['departstr'] = $this->pagination->create_links();
		$data['words'] = urldecode($keywords);
		$data['clearfix'] = 'caipu';
		$this->load->view('search',$data);
	}

	public function _shicai($keywords = '', $page = 1)
	{
		$keywords = urldecode($keywords); 
		$page = max(1, intval($page));
		$limit = 10; //每页显示数
		$offset = ($page - 1) * $limit;

		$query = $this->shicai_mdl->get_shicai_search($keywords, $offset, $limit);
		$search_count = $query->num_rows();

		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('search/caipu');
		$config['total_rows'] = $search_count;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config);

		$data['search_list'] = $query->result_array();
		$data['search_list_count'] = $search_count;
		$data['departstr'] = $this->pagination->create_links();
		$data['words'] = $keywords;
		$data['clearfix'] = 'shicai';

		$this->load->view('search',$data);
	}

	/**
	 * 用户
	 * @return [type] [description]
	 */
	public function _user($keywords = '', $page = 1)
	{
		$keywords = urldecode($keywords); 
		$page = max(1, intval($page));
		$limit = 10; //每页显示数
		$offset = ($page - 1) * $limit;

		$query = $this->caipu_mdl->get_caipu_search($keywords, $offset, $limit);
		$search_count = $query->num_rows();

		/* 分页配置 */
		$this->load->library('pagination');
		$config['base_url'] = site_url('search/caipu');
		$config['total_rows'] = $search_count;
		$config['per_page'] = $limit; 
		$this->pagination->initialize($config);

		$data['search_list'] = $query->result_array();
		$data['search_list_count'] = $search_count;
		$data['departstr'] = $this->pagination->create_links();
		$data['words'] = $keywords;
		$data['clearfix'] = 'user';

		$this->load->view('search',$data);
	}


}

/* End of file search.php */
/* Location: ./application/controllers/search.php */